#include "grid.h"
#include <iostream>

using namespace cv;
using namespace std;

grid::grid(Mat image,int _nB, char* _label){
	this->label = _label;
	this->nBlock = _nB;

	//Mat image = imread(_nameImage); 
	int n = _nB; // for instance n=8;
	int unitWidth = image.cols / n;  // you had image.rows / n;
	int unitHeight = image.rows / n;

	gridItem _initialNew(0,0,0,0);
	vector<gridItem> _tempGrid(n*n,_initialNew);
	int count = 0;

	for(int i = 0; i < n; i++) {  //i is row index
		// inner loop added so that more than one row of tiles written
		for(int j = 0; j < n; j++) { // j is col index
			//Take the next tile in the nxn grid. Unit is the width and height of
			//each tile. i%n and i/n are just fancy ways of a double x,y for loop

			// Mat subImage = image(Rect((i % n) * unitWidth, (i / n) * unitHeight, unitWidth, unitHeight));
			// I didn't understand the above line, as ((i % n)==i and (i / n)==0.
			// 
			Mat subImage = image(Rect(j * unitWidth, i * unitHeight, unitWidth, unitHeight));
			/*ostringstream oss;
			oss << i << "_" << j << ".jpg";
			string name = oss.str();
			imwrite(name, subImage);*/

			cv::Rect roi(j * unitWidth, i * unitHeight, unitWidth, unitHeight);
			//cout << "(i, j) = (" << i << ", " << j << ")" << endl;
			//cout << "(i %n) = " << i%n  << endl;
			//cout << "(i/n) = " << i/n << endl;
			//cout << "roi.x = " << roi.x << endl;
			//cout << "roi.y = " << roi.y << endl;
			//cout << "roi.width = " << roi.width << endl;
			//cout << "roi.height = " << roi.height << endl;
			//Mat _subImage = image(roi);

			gridItem _item(roi.x,roi.y,roi.width,roi.height);
			_tempGrid[count].assign(_item);

			count ++;
		}
	}

	this->listItem = _tempGrid;

}

int grid::getNBlock(){
	return this->nBlock * this->nBlock;
}

char* grid::getLabel(){
	return this->label;
}

std::vector<gridItem> grid::getListItem(){
	return this->listItem;
}
gridItem grid::getItem(int _index){
	return listItem.at(_index);
}

grid::grid(Rect _roi,int _nB, char* _label){
	this->label = _label;
	this->nBlock = _nB;

	//Mat image = imread(_nameImage); 
	int n = _nB; // for instance n=8;
	int unitWidth = _roi.width / n;  // you had image.rows / n;
	int unitHeight = _roi.height / n;

	gridItem _initialNew(0,0,0,0);
	vector<gridItem> _tempGrid(n*n,_initialNew);
	int count = 0;

	for(int i = 0; i < n; i++) {  //i is row index
		// inner loop added so that more than one row of tiles written
		for(int j = 0; j < n; j++) { // j is col index
			//Take the next tile in the nxn grid. Unit is the width and height of
			//each tile. i%n and i/n are just fancy ways of a double x,y for loop

			// Mat subImage = image(Rect((i % n) * unitWidth, (i / n) * unitHeight, unitWidth, unitHeight));
			// I didn't understand the above line, as ((i % n)==i and (i / n)==0.
			// 
			//Mat subImage = image(Rect(j * unitWidth, i * unitHeight, unitWidth, unitHeight));
			
			cv::Rect roi(j * unitWidth + _roi.x, i * unitHeight + _roi.y, unitWidth, unitHeight);

			gridItem _item(roi.x,roi.y,roi.width,roi.height);
			_tempGrid[count].assign(_item);

			count ++;
		}
	}

	this->listItem = _tempGrid;

}